public class Visualise extends Thread {

	private RopeState rs;
	private Object lock;

	public Visualise(RopeState rs, Object lock) {

		this.rs = rs;
		this.lock = lock;
	
	}
	
	public void run() {
	
		while (true) {
		
			outputGraphics();
			
			try {
				Thread.sleep(100); 
			}
			catch(InterruptedException e) { }
		}
		
	}
	
	private void outputGraphics() {
	
		// Find out the position of the rope (syncronise to guard against writes)
		int ropeOffset;
		synchronized (lock) {
			ropeOffset = (rs.getRopePosition() / 200) + 5;
		}
		
		// Print the rope in position
		int i;
		for (i=0; i < ropeOffset; i++) System.out.print(" ");
		System.out.print(" o o o           o o o \n");
		for (i=0; i < ropeOffset; i++) System.out.print(" ");
		System.out.print("~+-+-+-----*-----+-+-+~\n");
		for (i=0; i < ropeOffset; i++) System.out.print(" ");
		System.out.print(" ^ ^ ^           ^ ^ ^ \n");
		
		// Print the surroundings
		System.out.println("````````````````|````````````````");
		System.out.println("       Us       |      Them      ");
		System.out.println("                |                ");
		
		// Print the forces
		String myForce = "    " + rs.getMyForce();
		myForce = myForce.substring(myForce.length() - 4, myForce.length());
		String oppForce = "    " + rs.getOpponentForce();
		System.out.println("    "+myForce+" AEg    |    "+oppForce+" AEg");
		
		// Print the rope force and position
		System.out.println("                |                ");
		System.out.println("             Overall             ");
		System.out.println("     Rope Force = "+rs.getRopeForce()+" AEg");
		System.out.println("  Rope Position = "+rs.getRopePosition()+" AEg");
		System.out.println("   (AEg -> Afro-Euro grammes)\n");
		
		if (ropeOffset == 0)
			System.out.println("             We win!\n\n");
		else if  (ropeOffset == 10)
			System.out.println("            They win!\n\n");
		else
			System.out.println("\n\n");
		
		
	}

}